# Exercicio 8 - Regresses Multiplas
# Camila Galheigo Coelho 

setwd("C:/Users/Camila/Documents/PESQUISA/1_DOUTORADO/Disciplinas/R")
getwd()

# Galileu estava certo? #
init.h = c(600, 700, 800, 950, 1100, 1300, 1500)
h.d = c(253, 337, 395, 451, 495, 534, 573)

modelo.linear<-lm(h.d~init.h) 
plot(h.d~init.h)
abline(modelo.linear)

modelo.2o.grau<-lm(h.d~init.h+I(init.h^2)) #2o grau
?curve
curve(coef(modelo.2o.grau)[1]+coef(modelo.2o.grau)[2]*x+coef(modelo.2o.grau)[3]*x^2, add=T, lty=2)

modelo.3o.grau<-lm(h.d~init.h+I(init.h^2)+I(init.h^3)) #3o grau
curve(oef(modelo.3o.grau)[1]+oef(modelo.3o.grau)[2]*x+oef(modelo.3o.grau)[3]*x^2+oef(modelo.3o.grau)[4]*x^3, add=T, lty=3)

summary(modelo.linear)
summary(modelo.2o.grau)
summary(modelo.3o.grau)

anova(modelo.linear,modelo.2o.grau,modelo.3o.grau) 

#a menor soma dos quadrados dos resduos  a do modelo de 3o grau e portanto este  o que melhor.


# Recem nascidos #
4

RN.bruto<-read.table("babies.txt",head=TRUE,as.is=TRUE)
head(RN)
RN<-RN.bruto[RN.bruto$bwt!=999 & RN.bruto$gestation!=999 & RN.bruto$parity!=9 & RN.bruto$height!=99 & RN.bruto$smoke!=9 &RN.bruto$age!=99& RN.bruto$weight!=999,]
head(RN)
tail(RN)
str(RN)

#modelo durao da gestao
mod.gest<-lm(RN$bwt~RN$gestation)
plot(RN$bwt~RN$gestation, ylab="birth weight (pounds)", xlab="time of gestation (days)")
abline(mod.gest)

#modelo paridade
RN$parity<-as.factor(RN$parity)
mod.par<-lm(RN$bwt~RN$parity)
plot(RN$bwt~RN$parity, ylab="birth weight (pounds)", xlab="parity (number of births)")

#modelo idade da me
mod.age<-lm(RN$bwt~RN$age)
plot(RN$bwt~RN$age, ylab="birth weight (pounds)", xlab="mother's age (years)")
abline(mod.age)

#modelo altura da me
mod.alt<-lm(RN$bwt~RN$height)
plot(RN$bwt~RN$height, ylab="birth weight (pounds)", xlab="mother's height(cm)")
abline(mod.alt)

#modelo peso da me
mod.peso<-lm(RN$bwt~RN$weight)
plot(RN$bwt~RN$weight, ylab="birth weight (pounds)", xlab="mother's weight")
abline(mod.peso)

#modelo me fumante
RN$smoke<-as.factor(RN$smoke)
mod.fumante<-lm(RN$bwt~RN$smoke)
plot(RN$bwt~RN$smoke, ylab="birth weight (pounds)", xlab="mother smokes")

mod.todos<-lm(RN$bwt~RN$gestation+RN$smoke+RN$height+RN$parity+RN$weight+RN$age)

anova(mod.gest,mod.par,mod.age,mod.alt,mod.peso,mod.fumante,mod.todos)
# o melhor modelo  o mod.total

AIC(mod.gest,mod.par,mod.age,mod.alt,mod.peso,mod.fumante,mod.todos)
# de acordo com o AIC o modelo com tempo de gestao (mod.gest) explica quase to bem quanto o mod.todos,
#porm "pagando" menos graus de liberdade.
save.image()